﻿@model  RBAC.Web.Models.UserModel
@using RBAC.Web.CustomHelpers
@using RBAC.Web.Mapper
@using RBAC.Web.Utilities

@{
    var userProfile = Session["UserProfile"] as RbacLib.Models.UserProfile;
    var @privilageUpdateUser = RbacUtil.SetUpdateUserButton(userProfile);
    var @privilageDeleteUser = RbacUtil.SetDeleteUserButton(userProfile);

    //privilageUpdateUser = string.Empty;
    //privilageDeleteUser = string.Empty;
}

@{
    ViewBag.Title = "Edit User";

    string returnUrl = Url.Action("ListUser", "User");
}
@section Styles {
    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/form.js?3")" type="text/javascript"></script>
    <link href="@Url.Content("~/Content/themes/base/jquery.ui.all.css")" rel="stylesheet" />
    <link href="@Url.Content("~/Content/themes/base/jquery-ui.css")" rel="stylesheet" type="text/css" />
    <link href="@Url.Content("~/Content/themes/base/jquery.ui.datepicker.css")" rel="stylesheet" type="text/css" />
    @*<link href="@Url.Content("~/Content/site2.css")" rel="stylesheet" type="text/css" />*@

    <script type="text/javascript">
        $(function () {
            //How to setup Datepicker
            SetDatePicker("CreateDate", new Date(), '-71Y', 'c-70:c');
            SetDatePicker("FreeDate");
            
            //How to setup Lookup
            SetExternalLookupFor("User_Role", "User_RoleGetExternalLookup", 
"User_RolePopExternalLookup", 'User_Role|User_RoleLookupField|RoleId');
			
			
            //Master Detail
			 
							 
							 
							 
							 
							 
							 
							 
        });
    </script>
}

<h2>User Detail</h2>
@using (Html.BeginForm("Update", "User", FormMethod.Post, new { @id = "form-User", enctype = "multipart/form-data" }))
{
    @Html.ValidationSummary(false)
    <fieldset>
        <legend>UserModel</legend>


        <div class="two-column-container">
            @Html.HiddenFor(model => model.Id)


            <div class="texteditor">
                @Html.LabelFor(model => model.Username)
                @Html.TextBoxFor(model => model.Username)
                @Html.HiddenFor(model => model.Id)
                <input type="hidden" name="Mode" value="Edit" />
                <input type="hidden" name="OldValue" value="@(Model.Username == null ? "" : Model.Username)" />
            </div>


            <div class="texteditor">
                @Html.LabelFor(model => model.Password)
                @Html.TextBoxFor(model => model.Password)
            </div>


            <div class="texteditor">
                @Html.LabelFor(model => model.Email)
                @Html.TextBoxFor(model => model.Email)
            </div>


            <div class="texteditor">
                @Html.LabelFor(model => model.Mobile)
                @Html.TextBoxFor(model => model.Mobile)
            </div>


            <div class="texteditor">
                @Html.LabelFor(model => model.CreateDate)
                @Html.DatePickerFor(model => model.CreateDate, "CreateDate",true)
            </div>


            @* START ถ้าไม่มี lookup ไปหาแม่ Role, ClildClass=False, LinkToParent=False *@


            @*  chayawooth.puy
             <div class="texteditor">
                @Html.LabelFor(model => model.RoleId)
                @Html.TextBoxFor(model => model.RoleId, new { @readonly = "readonly", @class = "txtFillNameExternal" })
            </div>*@

            <div class="texteditor">
                @Html.LabelFor(model => model.Role)
                @Html.ExternalLookup(model => model.RoleRoleCode, "User_Role").For(model => model.Role).Render()
            </div>
            @* END ถ้าไม่มี lookup ไปหาแม่ *@




            <div class="texteditor">
                @Html.LabelFor(model => model.UserStatus)
                @Html.RadioButtonForSelectList(model => model.UserStatus, UserStatusMapper.GetUserStatusSelectList())
            </div>

        </div>
        <div class="bottom-menu clear">
            <input type="button" name="btnBack" value="Cancel" id="button-back" onclick="location.href='@returnUrl    '"/>
            <input type="Button" value="Save" id="button-save" onclick="JavaScript: SaveButtonClick()" @privilageUpdateUser />
            <input type="button" name="btnDelete" value="Delete" id="button-delete" onclick="JavaScript: DeleteButtonClick()" @privilageDeleteUser />
        </div>
    </fieldset>

    // Master Detail Dest Class
}

<div id="confirm-delete" title="Confirm delete." style="display: none">
    <span>Do you want to delete?</span>
</div>

<div id="confirm-save" title="Confirm save." style="display: none">
    <span>Do you want to save?</span>
</div>

<script type="text/javascript">
    function DeleteButtonClick() {
        $("#confirm-delete").dialog({
            modal:true,
            buttons: {
                Yes: function () {
                    $(this).dialog("close");
                    $.post('@Url.Action("DeleteUserById", "User")',$("#form-User").serialize(),function (data) {
                        var s = '@returnUrl'
                        if(s.indexOf("?") !== -1)
                            window.location.href = s + '&result=' + data + '&operation=delete';   
                        else
                            window.location.href = s + '?result=' + data + '&operation=delete'; 
                    });
                },
                No:function(){
                    $(this).dialog("close");
                }
            }
        })
    }

    function SaveButtonClick() {
        $("#confirm-save").dialog({
            modal: true,
            buttons: {
                Yes: function () {
                    $(this).dialog("close");
                    $("#form-User").submit();
                },
                No: function () {
                    $(this).dialog("close");
                }
            }
        })
    }
</script>
